package cn.com.self.apis.portal.controller;


import cn.com.self.apis.core.vo.ResultModel;
import cn.com.self.apis.portal.entity.SysRoleEntity;
import cn.com.self.apis.portal.service.intf.ISysRoleService;
import cn.com.self.apis.portal.service.intf.ISysUserRoleService;
import cn.com.self.apis.portal.service.intf.ISysUserService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.self.apis.portal.vo.TreeNodesModel;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.Constants;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataList;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import cn.com.selfsoft.smartcity.core.controller.GenericControllerImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* <p>
    * 系统角色 前端控制器
    * </p>
*
* @since 2019-09-27
*/
@Api(tags = "角色信息")
@Controller
@RequestMapping("/role")
public class SysRoleController extends GenericControllerImpl<SysRoleEntity, String> {
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysUserRoleService sysUserRoleService;
    @Autowired
    private ISysUserService sysUserService;

    @Override
    @RequestMapping(value = {"/count"}, method = RequestMethod.GET)
    public ResponseEntity count(HttpServletRequest request) {
        Map<String, Object> queryMap = ParameterUtil.HumpToUnderline(request.getParameterMap(),"mark","sort","access_token");
        Long total = this.sysRoleService.count(queryMap);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(total));
    }

    /**分页获取角色列表数据
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "分页获取角色列表数据")
    @ResponseBody
    @RequestMapping(value = {"/page"}, method = RequestMethod.GET)
    public ResponseEntity page(HttpServletRequest request, Page pageable) {

        //获取用户id
        String userid = request.getParameter("userid");
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "page", "size","access_token");

        if(StringUtils.isNotEmpty(userid)){
            queryParams.put("userid",userid);
            List<SysRoleEntity> dataList = sysRoleService.selectRoleList(queryParams);
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(dataList));
        }else{
            String page = request.getParameter("page");
            pageable.setCurrent(Long.parseLong(page));
            //分页获取角色数据
            ICustomPage<SysRoleEntity> dataList = this.sysRoleService.selectPage(queryParams, pageable);
            PageImpl<SysRoleEntity> pageData = new PageImpl<>(dataList.getRows(),
                    PageRequest.of(Integer.parseInt( String.valueOf(dataList.getCurrent())) - 1,
                            Integer.parseInt(String.valueOf(dataList.getSize()))), dataList.getTotal());
            DataPage<SysRoleEntity> dataPage = new DataPage<>(pageData);
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(dataList));
        }
    }

    @Override
    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    public ResponseEntity list(HttpServletRequest request, Sort sort) {
        Map<String, Object> queryMap = ParameterUtil.HumpToUnderline(request.getParameterMap(),"mark","sort","access_token");
        List<SysRoleEntity> list = this.sysRoleService.list(queryMap, sort);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataList(list)));
    }

    /**
     * 根据id获取角色
     */
    @ApiOperation(value = "根据id获取角色")
    @Override
    @RequestMapping(value = {"/load/{id}"}, method = RequestMethod.GET)
    public ResponseEntity load(@PathVariable("id") String id, HttpServletRequest request) {
        SysRoleEntity sysRoleEntity = this.sysRoleService.load(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(sysRoleEntity)));
    }

    /**删除角色信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "删除角色信息")
    @Override
    @RequestMapping(value = {"/deleteInfo/{id}"}, method = RequestMethod.DELETE)
    public ResponseEntity delete(@PathVariable("id") String id, HttpServletRequest request) {
        this.sysRoleService.delete(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

    /**批量删除角色信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "批量删除角色信息")
    @RequestMapping(value = {"/deletes"}, method = RequestMethod.DELETE)
    public ResponseEntity deletes(@RequestBody List<Long> ids) {
        this.sysRoleService.deletes(ids);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

    /**
     * 新增角色
     */
    @ApiOperation(value = "新增角色")
    @Override
    @RequestMapping(value = {"/create"}, method = RequestMethod.POST)
    public ResponseEntity create(@RequestBody SysRoleEntity entity, HttpServletRequest request) {
        SysRoleEntity sysRoleEntity = this.sysRoleService.create(entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(sysRoleEntity)));
    }

    /**
     * 修改角色
     */
    @ApiOperation(value = "修改角色")
    @Override
    @RequestMapping(value = {"/update/{id}"}, method = RequestMethod.POST)
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody SysRoleEntity entity, HttpServletRequest request) {
        SysRoleEntity localSysRoleEntity=this.sysRoleService.load(id);
        CopyUtils.copyProperties(entity, localSysRoleEntity);
        SysRoleEntity sysRoleEntity = this.sysRoleService.update(localSysRoleEntity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(sysRoleEntity)));
    }


     /**构建角色树
      * @Description //TODO
      * @return
      **/
     @ApiOperation(value = "构建角色树")
     @RequestMapping(value = "/tree", method = RequestMethod.GET)
     @ResponseBody
    public ResponseEntity createRoleTree(HttpServletRequest request){
         Map<String, Object> queryMap = ParameterUtil.HumpToUnderline(request.getParameterMap(),"mark","sort","access_token");
         TreeNodesModel treeNodesModel = sysRoleService.createRoleTree(queryMap);
         return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(treeNodesModel)));

     }



}
